package jdbc;

import java.sql.*;

public class JDBC {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.连接信息
        String url = "jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf8&useSSL=true";
        String username = "root";
        String pwd = "root";
        //3.获取数据库对象
        Connection connection = DriverManager.getConnection(url, username, pwd);
        //4.获取执行sql的对象
        Statement statement = connection.createStatement();
        //5.执行sql，获取结果
        String sql = "select * from agent";
        ResultSet resultSet = statement.executeQuery(sql);//查询用executeQuery，更新和删除使用executeUpdate
        while (resultSet.next()) {
            System.out.println("id:" + resultSet.getObject("id"));
            System.out.println("agent_num:" + resultSet.getObject("agent_num"));
            System.out.println("agent_name:" + resultSet.getObject("agent_name"));
            System.out.println("create_time:" + resultSet.getObject("create_time"));
            System.out.println("---------------------------------------------------------");
        }
        //6.关闭连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
